package com.hctforgreen.greenservice.ui.adapter;

import java.util.List;

import u.aly.bo;
import android.app.Activity;
import android.content.Context;
import android.content.IntentFilter;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import cn.dns.reader.network.Util;

import com.alibaba.fastjson.JSON;
import com.hctforgreen.greenservice.DownloadManagerActivityV2;
import com.hctforgreen.greenservice.R;
import com.hctforgreen.greenservice.DownloadManagerActivityV2.DownloadUpdateReceiver;
import com.hctforgreen.greenservice.ctr.DbController;
import com.hctforgreen.greenservice.model.StudyEntity;
import com.hctforgreen.greenservice.model.BookListEntity.BookEntity;
import com.hctforgreen.greenservice.utils.HctConstants;

/**
 * 学习资料拉适配器
 * 
 * @author dhb 20150502
 * 
 */
public class TerminalStudyListAdapter extends BaseAdapter {

	private Activity context;
	private List<StudyEntity> myList;
	private String downloadUrl;
	private ListView mListView;

	public TerminalStudyListAdapter(Activity context, List<StudyEntity> myList,
			ListView listView) {
		this.context = context;
		this.myList = myList;
		this.mListView = listView;
	}

	public int getCount() {
		return myList.size();
	}

	public Object getItem(int position) {
		return myList.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(
					R.layout.item_study_lst, null);
			holder.tvName = (TextView) convertView.findViewById(R.id.tv_name);
			holder.tvDate = (TextView) convertView.findViewById(R.id.tv_date);
			holder.tvDownloads = (TextView) convertView
					.findViewById(R.id.tv_downloads);
			holder.tvPraises = (TextView) convertView
					.findViewById(R.id.tv_praises);
			holder.imgExam = (ImageView) convertView
					.findViewById(R.id.has_exame);
			holder.imgDownload = (ImageView) convertView
					.findViewById(R.id.btn_download);
			holder.tvDownlaoded = (TextView) convertView
					.findViewById(R.id.tv_downloaded);
			holder.pView = convertView.findViewById(R.id.lyt_study_book);
			holder.hasExamIcon = (ImageView) convertView.findViewById(R.id.item_has_exam);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		final StudyEntity studyEntity = myList.get(position);
		holder.tvName.setText(studyEntity.title);
		holder.tvDate.setText(studyEntity.date);
		holder.tvDownloads.setText(context
				.getString(R.string.study_list_downloads)
				+ studyEntity.downloads);
		holder.tvPraises.setText(context.getString(R.string.study_list_praise)
				+ studyEntity.praises);
		//20161114 390697 add
		if (!TextUtils.isEmpty(studyEntity.examId)){
			//此份资料有考试
			holder.hasExamIcon.setVisibility(View.VISIBLE);
		}else{
			holder.hasExamIcon.setVisibility(View.GONE);
		}

		initStudyBook(holder, studyEntity);

		return convertView;
	}

	private void initStudyBook(final ViewHolder holder, StudyEntity entity) {
		if (TextUtils.isEmpty(entity.bookId)) {
			holder.imgDownload.setVisibility(View.INVISIBLE);
			holder.tvDownlaoded.setVisibility(View.GONE);
		} else {
			final DbController controller = new DbController(context);
			final BookEntity bookEntity = controller
					.getBookEntity(entity.bookId);
			if (bookEntity == null || TextUtils.isEmpty(bookEntity.id)) {
				holder.imgDownload.setVisibility(View.INVISIBLE);
				holder.tvDownlaoded.setVisibility(View.GONE);
			} else {
				if (bookEntity.bookZipState
						.equals(HctConstants.ON_ACTIVITY_KEY_BOOK_ZIP_STATE_DOWNLOAD_DONE)
						|| (HctConstants.ON_ACTIVITY_KEY_BOOK_ZIP_STATE_ANALISIS_DONE
								.equals(bookEntity.bookZipState) && bookEntity.bCanUpdate
								.equals(HctConstants.ON_ACTIVITY_KEY_BOOK_B_CAN_UPDATE_FALSE))) {// 已下载
					holder.imgDownload.setVisibility(View.GONE);
					holder.tvDownlaoded.setVisibility(View.VISIBLE);
				} else {// 未下载
					holder.imgDownload.setVisibility(View.VISIBLE);

					holder.tvDownlaoded.setVisibility(View.GONE);
					holder.imgDownload
							.setOnClickListener(new OnClickListener() {
								@Override
								public void onClick(View v) {// 点击下载资料
									holder.pView.setTag(bookEntity);
									bookEntity.doWhatClick = HctConstants.ON_ACTIVITY_KEY_DO_DOWNLOAD_CLICK;
									controller
											.updateBookOnWhatClick(bookEntity);

									/**
									 * 调用Service下载
									 */
									Util.doDownloadBook(context, bookEntity);

								}
							});
				}
			}
		}

	}

	static class ViewHolder {
		View pView;// itemview
		TextView tvName;// 名称
		TextView tvDate;// 时间
		TextView tvDownloads;// 下载
		TextView tvPraises;// 点赞
		ImageView imgExam;// 考试
		ImageView imgDownload;// 下载
		TextView tvDownlaoded;// 已下载
		ImageView hasExamIcon;//考试的icon
	}

	public void notifyDataSetChanged(String downloadUrl) {
		this.downloadUrl = downloadUrl;
		super.notifyDataSetChanged();
	}

	public void updateView(String downloadUrl) {

		int firstVisiblePosition = mListView.getFirstVisiblePosition();
		int lastVisiblePosition = mListView.getLastVisiblePosition();

		int visibleCount = (lastVisiblePosition - firstVisiblePosition);
		for (int i = 0; i <= visibleCount; i++) {
			View visibleView = mListView.getChildAt(i);

			if (visibleView == null) {
				continue;
			}
			View bookLyt = (View) visibleView.findViewById(R.id.lyt_study_book);
			if (bookLyt == null) {
				continue;
			}
			BookEntity tagBook = (BookEntity) bookLyt.getTag();
			if (tagBook != null && tagBook.download_url.equals(downloadUrl)) {

				final DbController dbController = new DbController(context);
				BookEntity inDbEntity = dbController
						.getBookEntityUseBookName(tagBook.bookName);
				if (HctConstants.ON_ACTIVITY_KEY_BOOK_ZIP_STATE_DOWNLOAD_DONE
						.equals(inDbEntity.bookZipState)
						|| (HctConstants.ON_ACTIVITY_KEY_BOOK_ZIP_STATE_ANALISIS_DONE
								.equals(inDbEntity.bookZipState) && inDbEntity.bCanUpdate
								.equals(HctConstants.ON_ACTIVITY_KEY_BOOK_B_CAN_UPDATE_FALSE))) {
					bookLyt.findViewById(R.id.btn_download).setVisibility(
							View.GONE);
					bookLyt.findViewById(R.id.tv_downloaded).setVisibility(
							View.VISIBLE);
				}
			}

		}

	}
}